package oj;

public class LC547 {
    public int findCircleNum(int[][] isConnected) {
        int count=0;
        for(int i=0;i<isConnected.length;i++){
            for(int j=0;j<isConnected[0].length;j++){
                if(isConnected[i][j]==1){
                    dfs(isConnected,i,j);
                    count++;
                }
            }
        }
        return count;
    }
    private void dfs(int[][] isConnected,int x,int y){
        if(x<0 || y<0 || x>=isConnected.length || y>=isConnected.length || isConnected[x][y]==0){
            return;
        }

        isConnected[x][y]=0;
        dfs(isConnected,x+1,y);
        dfs(isConnected,x,y+1);
        dfs(isConnected,x-1,y);
        dfs(isConnected,x,y-1);
    }
}
